package com.feeyin.radar.crm.controller;

import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.service.AuditManageService;
import com.feeyin.radar.crm.service.ContractService;
import com.feeyin.radar.crm.service.SaleCostService;
import com.feeyin.radar.sys.service.WarningService;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

public class AuditManageController extends MultiActionController
{
  private AuditManageService auditManageService;
  private WarningService warningService;
  private ContractService contractService;
  private SaleCostService saleCostService;
  private String auditManage;
  private String auditContract;
  private String auditSaleCost;
  private String auditDynamic;

  public AuditManageService getAuditManageService()
  {
    return this.auditManageService;
  }

  public void setAuditManageService(AuditManageService auditManageService) {
    this.auditManageService = auditManageService;
  }

  public WarningService getWarningService() {
    return this.warningService;
  }

  public void setWarningService(WarningService warningService) {
    this.warningService = warningService;
  }

  public ContractService getContractService() {
    return this.contractService;
  }

  public void setContractService(ContractService contractService) {
    this.contractService = contractService;
  }

  public SaleCostService getSaleCostService() {
    return this.saleCostService;
  }

  public void setSaleCostService(SaleCostService saleCostService) {
    this.saleCostService = saleCostService;
  }

  public String getAuditManage() {
    return this.auditManage;
  }

  public void setAuditManage(String auditManage) {
    this.auditManage = auditManage;
  }

  public String getAuditContract() {
    return this.auditContract;
  }

  public void setAuditContract(String auditContract) {
    this.auditContract = auditContract;
  }

  public String getAuditSaleCost() {
    return this.auditSaleCost;
  }

  public void setAuditSaleCost(String auditSaleCost) {
    this.auditSaleCost = auditSaleCost;
  }

  public String getAuditDynamic() {
    return this.auditDynamic;
  }

  public void setAuditDynamic(String auditDynamic) {
    this.auditDynamic = auditDynamic;
  }

  public ModelAndView display(HttpServletRequest request, HttpServletResponse response)
  {
    return new ModelAndView(getAuditManage());
  }

  public ModelAndView preAuditContract(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("result", request.getParameter("result"));
    String auditAllInvoice = (String)request.getSession().getAttribute("isAuditAllInvoice");
    String auditDeptInvoice = (String)request.getSession().getAttribute("isAuditDeptInvoice");
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    int sign = 0;
    if ((!StringUtils.isEmpty(auditAllInvoice)) && ("1".equals(auditAllInvoice)))
      sign = 1;
    else if ((!StringUtils.isEmpty(auditDeptInvoice)) && ("1".equals(auditDeptInvoice))) {
      sign = 2;
    }
    Page page = new Page();
    page.getPageList(this.auditManageService.getPageAuditContractsCount(sign, userId).intValue(), null, request);
    model.put("contracts", this.auditManageService.getPageAuditContracts(sign, userId, page));
    return new ModelAndView(getAuditContract(), model);
  }

  public ModelAndView auditContract(HttpServletRequest request, HttpServletResponse response)
  {
    boolean result = false;
    int success = 0;
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String[] ids = request.getParameterValues("id");
    if ((ids != null) && (ids.length > 0)) {
      result = this.auditManageService.auditContract(ids, userId);
      if (result) success = 1;
    }
    return new ModelAndView(new RedirectView("./auditManage.do?action=preAuditContract&result=" + success));
  }

  public ModelAndView returnContract(HttpServletRequest request, HttpServletResponse response)
  {
    boolean result = false;
    int success = 0;
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String[] ids = request.getParameterValues("id");
    if ((ids != null) && (ids.length > 0)) {
      result = this.auditManageService.returnContract(ids, userId);
      if (result) success = 1;
    }
    return new ModelAndView(new RedirectView("./auditManage.do?action=preAuditContract&result=" + success));
  }

  public ModelAndView preAuditSaleCost(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("result", request.getParameter("result"));
    String auditAllInvoice = (String)request.getSession().getAttribute("isAuditAllInvoice");
    String auditDeptInvoice = (String)request.getSession().getAttribute("isAuditDeptInvoice");
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    int sign = 0;
    if ((!StringUtils.isEmpty(auditAllInvoice)) && ("1".equals(auditAllInvoice)))
      sign = 1;
    else if ((!StringUtils.isEmpty(auditDeptInvoice)) && ("1".equals(auditDeptInvoice))) {
      sign = 2;
    }
    Page page = new Page();
    page.getPageList(this.auditManageService.getPageAuditSaleCostsCount(sign, userId).intValue(), null, request);
    model.put("saleCosts", this.auditManageService.getPageAuditSaleCosts(sign, userId, page));
    return new ModelAndView(getAuditSaleCost(), model);
  }

  public ModelAndView auditSaleCost(HttpServletRequest request, HttpServletResponse response)
  {
    boolean result = false;
    int success = 0;
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String[] saleCostCodes = request.getParameterValues("saleCostCode");
    if ((saleCostCodes != null) && (saleCostCodes.length > 0)) {
      result = this.auditManageService.auditSaleCost(saleCostCodes, userId);
      if (result) success = 1;
    }
    return new ModelAndView(new RedirectView("./auditManage.do?action=preAuditSaleCost&result=" + success));
  }

  public ModelAndView returnSaleCost(HttpServletRequest request, HttpServletResponse response)
  {
    boolean result = false;
    int success = 0;
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String[] saleCostCodes = request.getParameterValues("saleCostCode");
    if ((saleCostCodes != null) && (saleCostCodes.length > 0)) {
      result = this.auditManageService.returnSaleCost(saleCostCodes, userId);
      if (result) success = 1;
    }
    return new ModelAndView(new RedirectView("./auditManage.do?action=preAuditSaleCost&result=" + success));
  }

  public ModelAndView auditDynamic(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    if (this.warningService.isUseAuditWarningByUserId(userId)) {
      String auditAllInvoice = "";
      String auditDeptInvoice = "";

      if (request.getSession().getAttribute("isAuditAllInvoice") != null) {
        auditAllInvoice = (String)request.getSession().getAttribute("isAuditAllInvoice");
      }
      if (request.getSession().getAttribute("isAuditDeptInvoice") != null) {
        auditDeptInvoice = (String)request.getSession().getAttribute("isAuditDeptInvoice");
      }
      int sign = 0;
      if ((!StringUtils.isEmpty(auditAllInvoice)) && ("1".equals(auditAllInvoice)))
        sign = 1;
      else if ((!StringUtils.isEmpty(auditDeptInvoice)) && ("1".equals(auditDeptInvoice))) {
        sign = 2;
      }
      Page page = new Page();
      page.getPageList(this.auditManageService.getPageAuditContractsCount(sign, userId).intValue(), Integer.valueOf(50000), request);
      model.put("auditContracts", this.auditManageService.getPageAuditContracts(sign, userId, page));
      page = new Page();
      page.getPageList(this.contractService.getPageMyContractsCount(null, "2", null, userId, null).intValue(), Integer.valueOf(50000), request);
      model.put("passContracts", this.contractService.getPageMyContracts(null, "2", null, userId, page, null));
      page = new Page();
      page.getPageList(this.contractService.getPageMyContractsCount(null, "3", null, userId, null).intValue(), Integer.valueOf(50000), request);
      model.put("returnContracts", this.contractService.getPageMyContracts(null, "3", null, userId, page, null));

      page = new Page();
      page.getPageList(this.auditManageService.getPageAuditSaleCostsCount(sign, userId).intValue(), Integer.valueOf(50000), request);
      model.put("auditSaleCosts", this.auditManageService.getPageAuditSaleCosts(sign, userId, page));
      page = new Page();
      page.getPageList(this.saleCostService.getPageSaleCostsCount(null, "2", userId).intValue(), Integer.valueOf(50000), request);
      model.put("passSaleCosts", this.saleCostService.getPageSaleCosts(null, "2", userId, page));
      page = new Page();
      page.getPageList(this.saleCostService.getPageSaleCostsCount(null, "3", userId).intValue(), Integer.valueOf(50000), request);
      model.put("returnSaleCosts", this.saleCostService.getPageSaleCosts(null, "3", userId, page));
    }
    return new ModelAndView(getAuditDynamic(), model);
  }
}